<template>
  <div class="topbar">
    <div class="wrap topbarwrap">
      <div class="left">欢迎来到叩丁严选商城</div>
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" width="24px" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" width="20" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="login-btn" v-else @click="changeIsShowLoginModel(true)">
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from "vuex";
import { WeiXinLoginApI } from "@/request/api";
export default {
  data() {
    return {
      
    };
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      cartTotal: (state) => state.userInfo.cartTotal,
      userInfo: (state) => state.userInfo.userInfo,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModel: "LoginModel/changeIsShowLoginModel",
      changeIsLogined: "loginStatus/changeIsLogined",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
  },
  async created() {
    let code = this.$route.query.code;
    if (code) {
      let res = await WeiXinLoginApI({
        code,
      });
      if (res.code == 0) {
        alert("扫码登陆成功");
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        this.changeIsLogined(true);
        this.$route.query.code && this.$router.push(this.$route.path);
        //获取用户信息并更新vuex的数据 - 扫码登录
        this.asyncChangeUserInfo();
      } else if (res.code == 400) {
        alert("二维码失败,请重新扫码!");
        this.changeIsShowLoginModel(true);
      } else if (res.code == 407) {
        alert("请使用手机号码再进行登陆");
        this.changeIsShowLoginModel(true);
        localStorage.setItem("uuid", res.uuid);
      }
    } else {
      let token = localStorage.getItem("x-auth-token");
      this.changeIsLogined(Boolean(token));
      if (token) {
        this.asyncChangeUserInfo();
      } else {
        this.initUserInfo();
      }
    }
  },
};
</script>
 
<style lang = "less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    text-align: center;
    .right {
      li {
        float: left;
        margin-left: 20px;
        cursor: pointer;
        &.login-btn {
          width: 124px;
          background-color: #0a328e;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
        img {
          vertical-align: middle;
          border-radius: 50%;
        }
      }
    }
  }
}
</style>